﻿//#region Private Variables
var arrDivInclude       = ['divInfoMess','divInfoFillter','divInfoAction'];
var rowsPerPage = 2;
var totalRows = 0;
//var arrInfotions=[]; //cache data
var _Infoid = -1;

/*--------------------------------------------------------------------------------------------*/
//Loading data
function initListInfos() {
    var oInfotions = null;
    HAAT.Web.Cmsadmin.Modules.Info.SearchAll(function(infos) {      
        lstInfo = infos.value;
        //arrFunctions = lstFuncs;
        totalRows = dataLength = lstInfo.length;
        if (dataLength > 0) {
            LoadFunctionOfPage(1, lstFuncs);
        } else {
            util.removeAllRows("functionGird", { filter:function(tr) {return (tr.id != "funcTemp");}});
            $("#trNoReSult").attr('style', 'display:');
            $("#divPaging").attr('style', 'display:none');
        }
    });
}
/*--------------------------------------------------------------------------------------------*/
// Load data with paging
function LoadFunctionOfPage(pageCurrent, functions){    
    var object = null;
    dataLength  = functions.length;
    vfrom       = (pageCurrent-1) * rowsPerPage;
    var id      = 0;
    var row     = 0;
    var css     = '';
    util.removeAllRows("functionGird", { filter:function(tr) {return (tr.id != "funcTemp");}});
    for (var i=0; i<rowsPerPage; i++){
        if((vfrom+i)<dataLength){
            object = functions[vfrom+i];
            id  = object.Id;
            css = (i%2== 0)?'Row_Odd':'Row_Round';
            
            util.cloneNode("funcTemp", { idSuffix:id });
            
            $("#tdName" + id).html((object.Parent==0)? "<b>" + object.Name + "</b>" : "&nbsp;&nbsp;<i>"+object.Name+"</i>");
            $("#tdFilePath" + id).html(object.Src);
            $("#tdArrange" + id).html(object.Arrange);
            
            $("#funcTemp" + id).attr('style', 'display:');
            $("#funcTemp" + id).attr('class', css);            
        }
    }
    initPaging("divPaging", dataLength, "gotoPage", 1);
}  

/*------ADD NEW ----------*/
/*--------------------------------------------------------------------------------------------*/
function Functon_Add()
{
    SHObjectAll("divFuncAction");
    //ShowPanel("divFuncAction");
    ClearFormFunction();    
    BindFunctionIsParent("slIsFunction");
    
}
/*--------------------------------------------------------------------------------------------*/
function Functon_Edit(id){
    _funcid = id;
    
    SHObjectAll("divFuncAction");
    
    HAAT.Web.Cmsadmin.Mng.MngFunction.GetFunctions(_funcid, function(entity){
        var ofunc = entity.value;
        $('#txtFunName').val(ofunc.Name);
        $('#txtFuncFile').val(ofunc.Src);
        BindFunctionIsParent("slIsFunction",ofunc.Parent);
        $('#txtArrange').val(ofunc.Arrange);
        $('#txtFuncRole').val(ofunc.Actionfile);        
    });    
}
/*--------------------------------------------------------------------------------------------*/
function Function_SaveOrUpdate(){   
    var entity; 
    if (verifyInfoFunction()){
        if (_funcid == -1){// NEW...
            entity = {
                Id:-1,
                Name:$('#txtFunName').val(),
                Src:$('#txtFuncFile').val(),
                Parent:$('#slIsFunction').val(),
                Arrange:$('#txtArrange').val(),
                Actionfile:$('#txtFuncRole').val()                
            }        
        }else{//EDITING...
            entity = {
                Id:_funcid,
                Name:$('#txtFunName').val(),
                Src:$('#txtFuncFile').val(),
                Parent:$('#slIsFunction').val(),
                Arrange:$('#txtArrange').val(),
                Actionfile:$('#txtFuncRole').val()                
            }          
        }
        HAAT.Web.Cmsadmin.Mng.MngFunction.SaveOrUpdate(entity, function(bResult){
            if (bResult.value){  
                ShowMessage("divFuncMess","tdMessage", msgAdd);              
                initListFunction();
                
            }else{
                ShowMessage("divFuncMess","tdMessage", msgAddErr);
            }
        });
    }
}
/*--------------------------------------------------------------------------------------------*/
function Functon_Del(){
    var listIDs = getChecked("chkFunction",3);
    if(listIDs==""){
        alert(msgNocheck);
        return;
    }
    if(confirm(msgConfirm)){
        HAAT.Web.Cmsadmin.Mng.MngFunction.Delete(listIDs, function(bResult){
            if (bResult.value){  
                //HiddenPanel("divFuncAction");
                ShowMessage("divFuncMess","tdMessage", msgDel);              
                initListFunction();
                
            }else{
                ShowMessage("divFuncMess","tdMessage", msgDelErr);
            }
        });
    }
}
/*--------------------------------------------------------------------------------------------*/
function Function_Cancel(){
    HiddenPanel("divFuncAction");
}
function Function_Reset(){
    ClearFormFunction();
}
/*--------------------------------------------------------------------------------------------*/
function ClearFormFunction(){
    $('#slIsFunction').val(0);
    $('#txtArrange').val('');
    $('#txtFunName').val('');
    $('#txtFuncFile').val('');
    $('#txtFuncRole').val('');
}
/*--------------------------------------------------------------------------------------------*/

function BindFunctionIsParent(combox, val){   
 
    var myOption = $("#" + combox);  
    HAAT.Web.Cmsadmin.Mng.MngFunction.GetFunctionIsParent(function(parents){
        BindCombox(parents.value, myOption, val, ["0","[ Chọn chức năng ]"]);
    });
}
/*--------------------------------------------------------------------------------------------*/
/* HAM PHUC VU PHAN TRANG */
/*--------------------------------------------------------------------------------------------*/
function initPaging(divPage, totalRows, functionName, currentPage) {
    $("#"+divPage).html(callPage(currentPage, 1 , totalRows, rowsPerPage, functionName));
    $("#"+divPage).attr('style', 'display:');
}
/*--------------------------------------------------------------------------------------------*/
function callJumpPage(divPage, pageCurrent, base, functionName) {
    // tu file pag.js (callPage)
    pt = callPage(pageCurrent, base, totalRows, rowsPerPage, functionName);
    $("#"+divPage).html(pt);
    $("#"+divPage).attr('style', 'display:');
}

/*--------------------------------------------------------------------------------------------*/
function gotoPage(pageCurrent, base, functionName) {
    LoadFunctionOfPage(pageCurrent, arrFunctions);
    callJumpPage("divPaging",pageCurrent,base,functionName);
}
/*--------------------------------------------------------------------------------------------*/
function verifyInfoFunction(){   
    
    var sx = $("#txtArrange");
    var name= $("#txtFunName");
    var filecn= $("#txtFuncFile");   
    
    if (sx.val() == ""){
        alert("Thứ tự không được rỗng.");
        sx.focus();
        return false;
    }
    if (name.val() == ""){
        alert("Tên chức năng không được rỗng.");
        name.focus();
        return false;
    }
    if (filecn.val() == ""){
        alert("File chức năng không được rỗng.");
        filecn.focus();
        return false;
    }    
    return true;
} 